<?
class Clarray{
	//stripslashes_deep
	public function __construct($path="",$type="pathstr"){
		if($type=="pathstr"){
			chdir($_REQUEST["dir"]);
            $path=(empty($path))?getcwd():$path;
	        $this->MkSandirobj($path);	
		}
	}
	public static function init(){
		//重设Request的值
		$_REQUEST = Clarray::StripslashesDeep($_REQUEST);
		Clarray::ReSetRequest( 
			array
			( 
			"dir"  => "I:/AppServ/www/_doll/filesApp",
			"open" => "show.php" 
			) 
		);
		
		$_REQUEST["find"]=str_replace(array(" "),array("+"),
		$_REQUEST["find"] );
		Clarray::DefaultRequest(
		array(
			array("find",
				 array("/()/s","/()/is")///*设数组为arr*/
				 ,""/*val_1*/ 
			) ,////*如果（$_REQUEST[find]不为空并且不在数组arr中）,设为原始值 	
			   // 其它情况都设为val_1比如为空或者在数组当中
			array("open",array("show.php"),"show.php"),
			array("page",array(""),1)
		));	
		
	}
    public static function StripslashesDeep($value)
    {
	    if( get_magic_quotes_gpc() )
	    {
             $value = is_array($value) ? array_map('Clarray::StripslashesDeep'
	         , $value) : (isset($value) ? stripslashes($value) : null);
	    }
    return $value;
    } 
	
	public static function GetRequestParameters($url,$name)
	{
	     $ms;	 
	     $result=preg_match("/(?:\?|&)" .$name 
		 . "\=([^&]*)(?:&|$)/is",$url,$ms); 
	     return $result?$ms[1]:NULL;	
    }
    
	public static function ReSetRequest($name,$defaultValue="EMPTY")
	{
		
		if(is_array($name))
		{
		    foreach($name as $k=>$v)
			{   if(empty($_REQUEST[$k]))
				{
					$para=Clarray::GetRequestParameters($_SERVER['HTTP_REFERER'],$k);
					if( !empty($para) ){
					$_REQUEST[$k]=$para; 
					}	else {
					$_REQUEST[$k]=$v;
					}
				}
			}
			return true;	
		}
		if(!empty($_REQUEST[$name]))  return true;
		$para=Clarray::GetRequestParameters($_SERVER['HTTP_REFERER'],$name);
	     if( !empty($para) ){
		      $_REQUEST[$name]=$para; 
		 }	else {
			  $_REQUEST[$name]=$defaultValue;
		 }	
	}
	
	public static function DefaultRequest($name,$arrCondition=array("EMPTY"),$value="EMPTY"){
		if(is_array($name)){
			foreach($name as $v)
			{    $n=$requestName=$v[0];
			     $arr=$v[1];
				 $para=Clarray::GetRequestParameters($_SERVER['HTTP_REFERER'],$n);
				$in_arr=$condition=in_array($para,$arr);
				 if(empty($_REQUEST[$n]) or $in_arr)
					{
						
						if( !empty($para) and !$in_arr){
						    $_REQUEST[$n]=urldecode($para); 
						}else{
							//if( $in_arr ){
							    $_REQUEST[$n]=urldecode($v[2]);	
							//}else{
							//   $_REQUEST[$n]=urldecode($v[3]);	
							//}
						}
					}	
			}
			
		    return true;	
		}
		if( $_REQUEST[$name]=="" or in_array($arrCondition)){
		    $_REQUEST[$name]= urldecode($value);	
		}else{$_REQUEST[$name]=urldecode($_REQUEST[$name]);}
		
	}
	

	public static  function session($name){
        if(empty($_SESSION[$name])){
			return $default;
			}else{
		    return 	$_SESSION[$name];	
			}
	}
	public $objsandir;
	public $arrFiles;//这个成员是不停地改变的
	public function MkSandirobj($path=""){
		if(empty($this->objsandir)){
	    $this->objsandir=new Clsandir($path,0);
		}
		return $this->objsandir;
	}	
	public function MkDirLinks(){	
	    $dirs=$this->objsandir->get_complete_dirs() ;
		Clarray::FormatDirs($dirs);
		echo '<br><a href="javascript:$(\'.dirs\').toggle();void(0);">s/h</a><div style="display:none;" class="dirs">';
		echo _array_fill_table($dirs,"65px");
		echo '</div><br>';
	}
	public function SetFileLinks($arrFiles=""){	
	    //$arr为文件名的数组列表
	    $arrFiles=empty($arrFiles)?$this->objsandir->get_complete_files():$arrFiles ;
		if($_REQUEST['find'] ){ 
			$arrFiles=Clarray::ArrayFindElementByRegExp($arrFiles,$_REQUEST['find']) ;
			Clarray::FormatFilesFind($arrFiles);
		}else{
			Clarray::FormatFiles($arrFiles);
		}
		$this->arrFiles=$arrFiles;
	}
	public static function ArrayFindElementByRegExp($arr,$str="/[\.]/"){
		 
		if(!is_array($arr)){return array("Is not a array!");}
		$retarr=array();
		foreach($arr as $k=>$v){
			if( preg_match( urldecode($str) ,$v,$ms) ){
				//echo  ($str);
				$retarr[$k]=$v;
			}
		}
		return $retarr;	
	}
	public function MkFilelinksAndPageLinks($pageSize=26){
			
		$requestParas='&find='.urlencode($_REQUEST['find'])
		.'&open='.$_REQUEST['open'].'&dir='.
			  $_REQUEST['dir'].'&filename=';
		// urlencode   urldecode
		//输出pagelinks 	 1 2 3 4
	 	echo iconv('UTF-8','GB2312//IGNORE',  
		  _array_pagination($this->arrFiles,$_REQUEST["page"],$content,'',$requestParas,$pageSize,4) 
	 	);		
		//输出Filelinks
		echo _array_fill_table($content,"65px");
		//输出链接样式表
		echo _array_pagination_style();	
		
	}
	public static function FormatFiles(&$arr){
		if(!is_array($arr)){return array('Is not array!');}
		$retarr=array();
		foreach($arr as $k=>$v)
		{
		  $retarr[]='<a onclick="javascript:_gox_(\''._default_em($_REQUEST["open"],"show.php")
		  .'?open='.$_REQUEST['open'].'&dir='.
		  $_REQUEST['dir'].'&filename='.$v.
		  '\');void(0);"   target="main_frame">'.$v.'</a>'."\n";	
		}
		$arr=$retarr;
		return $arr;	
    }
	//格式化数组，为找到的数组元素高亮
	public static function FormatFilesFind(&$arr){
		if(!is_array($arr)){return array('Is not array!');}
		$retarr=array();
		foreach($arr as $k=>$v)
		{
		  $retarr[]='<font style="font-size:22px; '.
		  'color:#339c33"><b>{</b></font><a onclick="javascript:_gox_(\''
		  ._default_em($_REQUEST["open"],"show.php")
		  .'?open='.$_REQUEST['open'].'&dir='.
		  $_REQUEST['dir'].'&filename='.urlencode($v).
		  '\');void(0);"   target="main_frame">'.
		  preg_replace(urldecode($_REQUEST['find']),
		  "<font style='color:#de0022;'><b>\\1</b></font>",
		  $v ).
		  '</a><font style="font-size:22px;color:#339c33"><b>}</b></font>'."\n";	
		}
	
		$arr=$retarr;
		return $arr;
	
    }
	public static function FormatDirs(&$arr){
		$retarr=array();
		foreach($arr as $k=>$v)
		{
		  $retarr[]='<a href="javascript:_go2_(\''
		  .'?open='.$_REQUEST['open'].'&dir='.
		  $v.'&filename='.
		  '\')"   target="left_frame">'.$v.'</a>'."\n";	
		}
		$arr=$retarr; return $arr;
		
    }
	
	public function GetVars()
    {
        return get_class_vars(get_class($this));
    }
	public  static function  GetMethods()
	{
		return get_class_methods(Clarray);
		
	}
	
}
?> 